#![feature(test)]

extern crate test;

#[cfg(test)]
mod bench {
    use test::{black_box, Bencher};

    use chatbox::decode::Decoder;
    use chatbox::encode::Encoder;
    use chatbox::grammarfile::*;
    use chatbox::testlib::grammars::linear_decimal;
    use chatbox::util::binaryfraction_between_0_and_1_to_bytes;

    #[bench]
    fn bench_encode_decode(b: &mut Bencher) {
        // let grammar = linear_with_powers_of_2();
        // more challenging variant:
        let grammar = linear_decimal();
        // let grammar = wrapped_nonempty_dyck1(0.2, 0.8);

        assert!(syntactically_validate_grammar(&grammar) == ValidationResult::Ok);

        b.iter(|| {
           for i in 0..1 {
                black_box({
                    // let input = b"\x00\x00\x00\x00\xff\xff\xff\xffshorter".to_vec();
                    // let input = b"\x00\x00\x00\x00\xff\xff\xff\xfflorem ipsum sic dolor amet adipiscing elit".to_vec();
                    let input = b"\x00\x00\x00\x00\xff\xff\xff\xfflonger text so we can see what's going on\xff\xff\xff\xff\x00\x00\x00\x00Lorem ipsum dolor sit amet, consectetur adipiscing elit".to_vec();
                    // let input = b"\x00\x00\x00\x00\xff\xff\xff\xffeven longer text so we can see what's going on\xff\xff\xff\xff\x00\x00\x00\x00Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.".to_vec();
                    println!("start {}", i);
                    let enc = Encoder::with_live_random(&grammar, &input[..]);
                    let encoded: Vec<Vec<u8>> = enc.collect();
                    println!("encoded = {:?}", &encoded);
                    let mut encoded_iter = encoded.into_iter();
                    let mut dec = Decoder::new(&grammar, &mut encoded_iter);
                    let res = dec.decode();
                    let res = res.unwrap();
                    let rb = binaryfraction_between_0_and_1_to_bytes(&res.l);
                    println!("decode = {:02X?}", &rb);
                    assert_eq!(&input[..], &rb[0..input.len()]);
                });
            }
        });
    }
}
